package com.zeus.base.smscenter.service.impl.common;

import com.zeus.base.smscenter.common.mapper.SmsCenterBasisMapper;
import com.zeus.base.smscenter.common.mapper.SmsCenterBasisMapper;
import com.zeus.base.smscenter.service.common.ISmsCenterBasisService;
import com.zeus.base.smscenter.service.common.ISmsCenterBasisService;
import com.zeus.base.privilege.common.model.BasisEntity;
import com.zeus.base.privilege.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 基础Service
 *
 * @author kangqi
 * @version 2016-11-11
 */
@Service
@Transactional(readOnly = true)
public class SmsCenterBasisServiceImpl implements ISmsCenterBasisService {
    @Autowired
    private SmsCenterBasisMapper smsCenterBasisMapper;
    @Override
    public Page<Map<String, Object>> findPage(BasisEntity t, Integer page, Integer pageSize) {
        if (page==null || page == 0) page = 1;
        if (pageSize == null) pageSize = 0;
        Page<Map<String, Object>> result = new Page<Map<String, Object>>();
        if (pageSize > 0) {
            //需要分页
            result.setPage(page);
            result.setPageSize(pageSize);
            page--;
            t.setStartIndex_(page * pageSize);
            t.setEndIndex_(pageSize);
            result.setRecords(smsCenterBasisMapper.findCount(t));
            if (result.getRecords() == null || result.getRecords() == 0)
                return result.init();//无数据
        }
        result.setRows(smsCenterBasisMapper.findPage(t));
        if (pageSize == 0)//不分页,总记录数直接取rows
            result.setRecords(result.getRows().size());
        if (t.getSum_() != null && !t.getSum_().equals("") && result.getRows().size() > 0) {
            result.setListSum(smsCenterBasisMapper.findSum(t));//合计
        }
        return result.init();
    }
}
